package com.yida.gwj.config.controller;

import com.yida.common.core.domain.R;
import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.api.domain.GwjConfCalculateType;
import com.yida.gwj.config.domain.dto.GwjConfCalculateTypeDTO;
import com.yida.gwj.config.service.IGwjConfCalculateTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 计算标准Controller
 * <p>
 * 修订20230112 hmh(修改地址，权限名,方法名替换，重复对象构建，提示信息修改，查询匹配姓名时返回多条以对象接收BUG)
 *
 * @author lqq
 * @date 2022-12-13
 */
@RestController
@RequestMapping("/calculateType")
public class GwjConfCalculateTypeController extends BaseController {

	@Autowired
	private IGwjConfCalculateTypeService gcCalculateTypeService;

	/**
	 * 查询计算标准列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("gwj:config:calculateType:list")
	public AjaxResult list(GwjConfCalculateType gcCalculateType) {
		gcCalculateType.setDelFlag(UserStatus.OK.getCode());
		List<GwjConfCalculateType> list = gcCalculateTypeService.selectGwjConfCalculateTypeList(gcCalculateType);
		return AjaxResult.success(GwjConvertUtil.convertList(list, GwjConfCalculateTypeDTO.class));
	}

	/**
	 * 查询计算标准列表
	 */
	@GetMapping("/unit/deposit/list")
	@RequiresPermissions("gwj:config:calculateType:list")
	public AjaxResult unitList(GwjConfCalculateType gcCalculateType) {
		gcCalculateType.setDelFlag(UserStatus.OK.getCode());
		List<GwjConfCalculateType> list = gcCalculateTypeService.selectGwjConfCalculateTypeList(gcCalculateType).stream().filter(item -> !"平摊标准".equals(item.getName())).collect(Collectors.toList());
		return AjaxResult.success(GwjConvertUtil.convertList(list, GwjConfCalculateTypeDTO.class));
	}

	/**
	 * 查询所有有效的计算标准列表
	 */
	@GetMapping("/allValid")
	public AjaxResult allValid() {
		GwjConfCalculateType confCalculateType = new GwjConfCalculateType();
		confCalculateType.setDelFlag(UserStatus.OK.getCode());
		confCalculateType.setStatus(UserStatus.OK.getCode());
		return AjaxResult.success(gcCalculateTypeService.selectGwjConfCalculateTypeList(confCalculateType));
	}

	/**
	 * 导出计算标准列表
	 */
	@PostMapping("/export")
	@RequiresPermissions("gwj:config:calculateType:export")
	@Log(title = "计算标准", businessType = BusinessType.EXPORT)
	public void export(HttpServletResponse response, GwjConfCalculateType gcCalculateType) {
		List<GwjConfCalculateType> list = gcCalculateTypeService.selectGwjConfCalculateTypeList(gcCalculateType);
		ExcelUtil<GwjConfCalculateType> util = new ExcelUtil<>(GwjConfCalculateType.class);
		util.exportExcel(response, list, "计算标准数据");
	}

	/**
	 * 获取计算标准详细信息
	 */
	@GetMapping(value = "/{calculateTypeId}")
	@RequiresPermissions("gwj:config:calculateType:query")
	public AjaxResult getInfo(@PathVariable("calculateTypeId") Long calculateTypeId) {
		return AjaxResult.success(gcCalculateTypeService.selectGwjConfCalculateTypeByCalculateTypeId(calculateTypeId));
	}

	/**
	 * 新增计算标准
	 */
	@PostMapping
	@RequiresPermissions("gwj:config:calculateType:add")
	@Log(title = "计算标准", businessType = BusinessType.INSERT)
	public AjaxResult add(@RequestBody GwjConfCalculateType gcCalculateType) {
		return toAjax(gcCalculateTypeService.insertGwjConfCalculateType(gcCalculateType));
	}

	/**
	 * 修改计算标准
	 */
	@PutMapping
	@RequiresPermissions("gwj:config:calculateType:edit")
	@Log(title = "计算标准", businessType = BusinessType.UPDATE)
	public AjaxResult edit(@RequestBody GwjConfCalculateType gcCalculateType) {
		return toAjax(gcCalculateTypeService.updateGwjConfCalculateType(gcCalculateType));
	}

	/**
	 * 删除计算标准
	 */
	@DeleteMapping("/{calculateTypeIds}")
	@RequiresPermissions("gwj:config:calculateType:remove")
	@Log(title = "计算标准", businessType = BusinessType.DELETE)
	public AjaxResult remove(@PathVariable Long[] calculateTypeIds) {
		return toAjax(gcCalculateTypeService.deleteGwjConfCalculateTypeByCalculateTypeIds(calculateTypeIds));
	}

	/**
	 * 获取计算标准详细信息
	 */
	@InnerAuth
	@GetMapping(value = "/inner/{calculateTypeId}")
	public R<GwjConfCalculateType> selectGwjConfCalculateTypeByCalculateTypeId(@PathVariable("calculateTypeId") Long calculateTypeId) {
		return R.ok(gcCalculateTypeService.selectGwjConfCalculateTypeByCalculateTypeId(calculateTypeId));
	}
}
